package com.fyzk.activity.lottery.jc.football.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.lottery.jc.football.JCInfo;
import com.fyzk.activity.lottery.jc.football.JCInfo.SPInfo;
import com.fyzk.activity.lottery.jc.football.JZBetContentUtil;
import com.fyzk.activity.lottery.jc.football.JZBetNumUtil;
import com.fyzk.activity.lottery.jc.football.guoguan.JC_GuoGuanDialog;
import com.fyzk.activity.lottery.zc.tz_dialog.ZCBF_infor;
import com.fyzk.activity.zhifu.JCBasePayActivity;
import com.fyzk.interfaces.OnFootballPayTeamDeleteListener;
import com.tyjc.lottery.R;
import com.fyzk.net.PublicMethod;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 初竞彩让球和胜平负 之外需要弹框选择比赛的Adapter
 *
 * @author miao
 */
public class JCZQ_BF_ZhiFuAdapter extends JCZQZhiFuBaseAdapter {

    private static final String TAG = "JCBF_ZhiFuAdapter";
    private int index;
    private boolean isDan;
    private Handler handler;
    private int zhuShu;
    private OnFootballPayTeamDeleteListener listener;
    private String chuanStr = JC_GuoGuanDialog.result;
    private int color_hui;
    private boolean isHG; // 是否是混合过关
    private Context context;
    private ArrayList<BFGridAdapter> bf_adapters = new ArrayList<>();

    public JCZQ_BF_ZhiFuAdapter(Context context, boolean type, List<JCInfo> infors, Handler
            handler, OnFootballPayTeamDeleteListener listener) {
        this.isDan = type;
        this.infors = getInfor(infors);
        this.context = context;
        this.handler = handler;
        this.listener = listener;
        color_hui = context.getResources().getColor(R.color.gray);
        refreshDan();
    }


    @Override
    public int getCount() {
        return infors.size();
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    @Override
    public Object getItem(int position) {
        return infors.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        index = position;
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_jz_bf_pay, null);
            holder = new ViewHolder();
            holder.zhu = convertView.findViewById(R.id.jcbf_zhifu_list_item_zhu);
            holder.delete = convertView.findViewById(R.id.jcbf_zhifu_list_item_delete);
            holder.home = convertView.findViewById(R.id.jcbf_zhifu_list_item_tv_home);
            holder.week_num = convertView.findViewById(R.id.week_num);
            holder.away = convertView.findViewById(R.id.jcbf_zhifu_list_item_tv_away);
            holder.dan = convertView.findViewById(R.id.jcbf_zhifu_list_item_dan);
            holder.fl_open = convertView.findViewById(R.id.jz_bf_pay_zhankai);
            holder.jzbf_grid = convertView.findViewById(R.id.jzbf_pay_grid);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        JCInfo infor = infors.get(position);
        if (isDan || isHG) {
            holder.dan.setVisibility(View.GONE);
        } else {
            holder.dan.setVisibility(View.VISIBLE);
            if (infor.isDaneble()) {
                holder.dan.setEnabled(true);
                holder.dan.setChecked(infor.isDan());
                holder.dan.setTag(R.string.jc_info, infor);
                holder.dan.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        JCInfo infor = (JCInfo) v.getTag(R.string.jc_info);
                        infor.setDan(!infor.isDan());
                        refreshDan();
                    }
                });
            } else {
                holder.dan.setChecked(false);
                holder.dan.setEnabled(false);
            }
        }

        holder.zhu.setVisibility(View.VISIBLE);
        holder.home.setText(infor.getHomeTeam());
        holder.away.setText(infor.getAwayTeam());

        holder.week_num.setText(infor.getOfficialWeekDay() + PublicMethod.getTeamNum(infor
                .getMatchNum()));

        holder.fl_open.setTag(R.string.jc_info, infor);
        holder.fl_open.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.open((JCInfo) v.getTag(R.string.jc_info));
                }
            }
        });

        holder.delete.setTag(R.string.jc_info, infor);
        holder.delete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                JCInfo infor = (JCInfo) v.getTag(R.string.jc_info);
                infor.setOnclikNum(0);
                Set<Map.Entry<String, SPInfo>> set = infor
                        .btnHGPojo.entrySet();
                for (Iterator<Map.Entry<String, SPInfo>> it = set
                        .iterator(); it.hasNext(); ) {
                    Map.Entry<String, SPInfo> entry = it
                            .next();
                    SPInfo pojo = entry.getValue();
                    pojo.isSel = false;

                }
                List<ZCBF_infor> touZhu = infor.getTouZhuInfor();
                for (int j = 0; j < touZhu.size(); j++) {
                    touZhu.get(j).setSelect(false);
                }
                if (listener != null) {
                    listener.onDelete(infor);
                }
                infor.setResult(RESULT_MOREN);
                setInfors(infors, isDan);
                jiSuanZhuShu();
                notifyDataSetChanged();
            }
        });

        jiSuanZhuShu();
        if (infor.getOnclikNum() > 0) {
            holder.jzbf_grid.setVisibility(View.VISIBLE);
            holder.fl_open.setText("");
            BFGridAdapter bfGridAdapter = new BFGridAdapter(infor, context);
            holder.jzbf_grid.setAdapter(bfGridAdapter);
        } else {
            holder.jzbf_grid.setVisibility(View.GONE);
            holder.fl_open.setText(R.string.open_betarea);
        }
        return convertView;
    }

    /**
     * 获得支付list数据
     *
     * @param infors
     * @return
     */
    private List<JCInfo> getInfor(List<JCInfo> infors) {
        List<JCInfo> list = new ArrayList<JCInfo>();
        for (int i = 0; i < infors.size(); i++) {
            JCInfo info = infors.get(i);
            if (!info.getResult().equals(RESULT_MOREN)) {
                list.add(info);
            }
        }
        return list;
    }

    /**
     * 计算注数（待修改）
     */
    public void jiSuanZhuShu() {
        List<Integer> clicknums = new ArrayList<Integer>();
        List<Boolean> dannums = new ArrayList<Boolean>();
        zhuShu = 0;
        int changCi = 0;
        int wanFa = 1;
        boolean isDanShi = true;
        for (int i = 0; i < infors.size(); i++) {
            JCInfo info = infors.get(i);
            int xuan_count = 0;
            if (!info.getResult().equals(RESULT_MOREN)) {
                changCi++;
                String result = info.getResult();
                if (result.contains(RESULT_SPLIT)) {
                    isDanShi = false;
                    String[] split = result.split(RESULT_SPLIT);
                    xuan_count = split.length;
                } else {
                    xuan_count = 1;
                }
            }

            clicknums.add(xuan_count);
            if (info.isDan()) {
                wanFa = 2;
            }
            dannums.add(info.isDan());// 添加胆拖
        }
        List<Integer> chuans = new ArrayList<Integer>();
        if (isDan) {
            for (int i = 0; i < clicknums.size(); i++) {
                zhuShu += clicknums.get(i);
            }
        } else {
            String chuan = JC_GuoGuanDialog.result;
            if (chuan.contains(",") || chuan.endsWith("串1")){
                if (chuan.contains("串")) {
                    String[] chuan_array = chuan.split(",");
                    for (int i = 0; i < chuan_array.length; i++) {
                        String chuan_i = chuan_array[i];
                        chuans.add(Integer.valueOf(chuan_i.charAt(0) + ""));
                    }

                    for (Integer i : chuans) {
                        zhuShu += JZBetNumUtil.getZhushu(i, clicknums, dannums);
                    }
                }
            } else if (chuan.contains("串")) {
                String[] split = chuan.split("串");
                int teamNum = Integer.valueOf(split[0]);
                int select = Integer.valueOf(split[1]);
//                zhuShu = JZBetNumUtil.getDuoZhushu(teamNum, select, dannums, clicknums);
//                // L场比赛选择M关的可能性为groupNum种组合情况
//                int groupNum = JZBetNumUtil.getZhushu(teamNum, clicknums, dannums);

                String betcode = JZBetContentUtil.getContentBF(infors, isDan, chuan);
                zhuShu = JZBetNumUtil.caculateZhushu(chuan, betcode, teamNum, isDan, dannums);

            }

        }

        Message msg = handler.obtainMessage();
        msg.obj = zhuShu;
        msg.arg1 = changCi;
        msg.arg2 = wanFa;
        msg.what = JCBasePayActivity.ZF_WHAT1;
        handler.sendMessage(msg);
    }


    public List<JCInfo> getInfors() {
        return infors;
    }

    public void setInfors(List<JCInfo> infors, boolean isdanguan) {
        this.isDan = isdanguan;
        this.infors = getInfor(infors);
        this.notifyDataSetChanged();
    }


    class ViewHolder {
        LinearLayout zhu;
        ImageView delete;
        TextView week_num;
        TextView home;
        TextView away;
        CheckBox dan;
        GridView jzbf_grid;
        TextView fl_open;
    }

}
